
function initPalette()
{
  var palette = $('palette');
  Object.extend( palette,
  {
    init: function()
    {
      this.hide();
      var bar2 = $('plt.bar.2');
      Event.observe( bar2, 'mousedown', this.mouseDownOnSpan.bindAsEventListener( this ), true );
      Event.observe( bar2, 'mouseup', this.mouseUpOnSpan.bindAsEventListener( this ), true );
      Event.observe( bar2, 'mousemove', this.moveSpan.bindAsEventListener( this ), true );
      
      var div = Element.extend( $('plt.fore') );
      var aa = div.getElementsByTagName('a');
      for(var i = 1; i <= aa.length; i++ )
      {
        var a = $('plt.fore.'+i);
        var color = a.style.backgroundColor;
        a.href = "javascript:globalPalette.colorSet('"+ color + "')"
      }
      
      div = Element.extend( $('plt.back') );
      aa = div.getElementsByTagName('a');
      for(var i = 1; i <= aa.length; i++ )
      {
        var a = $('plt.back.'+i);
        var color = a.style.backgroundColor;
        a.href = "javascript:globalPalette.bcolorSet('"+ color + "')"
      }

      div = Element.extend( $('plt.font.style') );
      aa = div.getElementsByTagName('a');
      for(var i = 1; i <= aa.length; i++ )
      {
        var a = $('plt.font.style.'+i);
        if( i == 4 )
          a.href = "javascript:globalPalette.textStyleSet('"+ a.title + "')"
        else
          a.href = "javascript:globalPalette.fontStyleSet('"+ a.title + "')"
      }

      div = Element.extend( $('plt.font.weight') );
      aa = div.getElementsByTagName('a');
      for(var i = 1; i <= aa.length; i++ )
      {
        var a = $('plt.font.weight.'+i);
        a.href = "javascript:globalPalette.fontWeightSet('"+ a.title + "')"
      }

      div = Element.extend( $('plt.font.size') );
      aa = div.getElementsByTagName('a');
      for(var i = 1; i <= aa.length; i++ )
      {
        var a = $('plt.font.size.'+i);
        a.href = "javascript:globalPalette.fontSizeSet('"+ a.title + "')"
      }
    },
    
    activate: function( iframe )
    {
      this.iframe = iframe;
      this.show();
    },
    
    deactivate: function()
    {
      this.hide();
    },
    
    mouseDownOnSpan: function( event )
    {
      this.mouseDown = 1;
      var pos = Position.cumulativeOffset( this ); // ( palette );
      this.xdown = Event.pointerX( event ) - pos[0]; // relative location of mouse's cursor
      this.ydown = Event.pointerY( event ) - pos[1];
      return false;
    },
  
    mouseUpOnSpan: function( event )
    {
      this.mouseDown=0;
      return true;
    },

    moveSpan: function( event )
    {
      if( this.mouseDown == 1 )
      {
        this.style.left = Event.pointerX( event ) - this.xdown + "px"
        this.style.top =  Event.pointerY( event ) - this.ydown + "px"
      }
    },
/*    
    getStyleSelectedElement: function()  // TODO: invoke only one time !!!!
    {
      try
      {
        var range = new CRange( this.iframe.contentWindow );
        return range.getSelectedElement().style; 
      }
      catch(e)
      {
       alert(e) 
      }
    },
*/
    setStyleForSelection: function( style, value, toggle )  
    {
      try
      {
        var range = this.iframe.range;
        range.activate();
        range.applyCSSToSelection( style, value, toggle ); 
      }
      catch(e)
      {
        alert(e);
      }
    },

    colorSet: function( color )
    {
      try
      {
//        this.getStyleSelectedElement().color = color;
        this.setStyleForSelection( 'color', color ); 
      }
      catch(e){}
    },
  
    bcolorSet: function( color )
    {
      try
      {
//        this.getStyleSelectedElement().backgroundColor = color; 
        this.setStyleForSelection( 'backgroundColor', color ); 
      }
      catch(e){}
    },

    fontStyleSet: function( st )
    {
      try
      {
//        this.getStyleSelectedElement().fontStyle = st; 
        this.setStyleForSelection( 'fontStyle', st ); 
      }
      catch(e){}
    },

    fontWeightSet: function( w )
    {
      try
      {
//        this.getStyleSelectedElement().fontWeight = w; 
        this.setStyleForSelection( 'fontWeight', w );
      }
      catch(e){}
    },

    fontSizeSet: function( size )
    {
      try
      {
//        this.getStyleSelectedElement().fontSize = size; 
        this.setStyleForSelection( 'fontSize', size ); 
      }
      catch(e){}
    },

    textStyleSet: function( s )
    {
      try
      {
//        var stle = this.getStyleSelectedElement();
        var stle = this.setStyleForSelection( 'textDecoration', s, 'none' ); 
//        var s_old = stle.textDecoration;
//        alert( s_old + ' : ' + s)
//        if( s_old || s_old == s )
//          stle.textDecoration = 'none'; 
//        else
//          stle.textDecoration = s; 
      }
      catch(e)
      {
        alert(e) 
      }
    }
    
  })
  palette.init();
  return palette;
}